import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/login',
    name: 'login',
    component: () =>
      import ('@/views/login')
  },
  {
    path: '/home',
    name: 'home',
    component: () =>
      import ('@/views/home'),
    //重定向
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      name: 'welcome',
      component: () =>
        import ('@/views/welcome')
    }]
  }
]

const router = new VueRouter({
    routes
  })
  //路由守卫
router.beforeEach((to, from, next) => {
  //非登录用户禁止访问后台页面，强制登录
  //用户没有的登录userinfo===null
  let userinfo = window.sessionStorage.getItem('userinfo')
  if (!userinfo && to.path !== '/login') {
    return next('/login')
  }
  next() //放行
})
export default router